package pierwszy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.io.Zip;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class Excercises {
	static WebDriver driver;
    static WebDriverWait wait;
    
    private String PortalLink = "http://www.bermar2.home.pl/ks_portal/login.php";
    private String login = "p.jaworek";
    private String password = "jawso98*";
    
	private String site = "ks_portal";
	private String iconPlusOptionCheck = "Płatności";
	private String iconPlusOption = "Zobowiązanie wobec US "; 
	private KsPortalPage KSP;
	private Utils UTL;

	
    @BeforeClass
    void setUp() {
    	System.out.println("Przed klasą");
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver,10);
        driver.manage().window().maximize();
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        UTL = new Utils(driver, wait);
        
    	driver.get(PortalLink);
    }
    
    @AfterClass
    static void tearDown() {
    	System.out.println("Za klasą");
    	//driver.quit();
        //driver.close();
    }
    
    @BeforeMethod
    void setUpTest(){
    	System.out.println("Przed metodą testową");
    }
    
    @AfterMethod
    void tearDownTest(){
    	System.out.println("Za metodą testowa");
    }
    
    @Test 
    public void mouseActions() throws InterruptedException, IOException{
  //Log in
  		LogInPage Login = new LogInPage(driver, wait); 
  		MainPage MP = Login.logIn(login,password);
  					
  		//Choose and Check KsPortal site	
  		KSP = MP.chooseSite(site);
  		
  		//Check if KsPortal was chosen.
  		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passSelectedSite(site)));
  		assert driver.findElement(UiMapping.passSelectedSite(site)).getText().trim().equals(site) : "Site " + site + " was not chosen.";
  		
  		//Choose and Check IconPlusOption (Employee)	
  		KSP.getOptionFromIconPlus(iconPlusOption);
  				
  		//Check if Employee option was chosen.
  		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.IconPlusCheck));
  		assert driver.findElement(UiMapping.IconPlusCheck).getText().equals(iconPlusOptionCheck) : "Option " + iconPlusOption + " was not chosen.";
    
  		UTL.mouseActions();
  		
  		UTL.makeScreenShoot();
  		
  		UTL.navigateSite();
  		
  		//cookies
    	
    	//driver.manage().deleteAllCookies();

  		driver.get("https://www.facebook.com");
  		
  		UTL.addCookies();
  		
  		driver.get("https://www.facebook.com");	
  		

    }
    
    @Test (dependsOnMethods = "mouseActions")
    public void WebDriverEvents(){
  		//WebDriver Events
    	EventFiringWebDriver eventFiringWebDriver =  new EventFiringWebDriver(driver);
  		MyEventListener myEventListener = new MyEventListener();
  		
  		//Register
  		eventFiringWebDriver.register(myEventListener);
  		
  		//Navigate to
  		eventFiringWebDriver.get("www.google.pl");
  		
  		//Navigate back
  		eventFiringWebDriver.navigate().back();  	
  		
  		//Navigate forward
  		eventFiringWebDriver.navigate().forward();
  		
  		//Find by
  		wait.until(ExpectedConditions.presenceOfElementLocated(By.id("hplogo")));
		WebElement googleInputFiled = eventFiringWebDriver.findElement(By.id("hplogo"));
		
		//Click on
		googleInputFiled.click();
		
		//Change value of
		googleInputFiled.sendKeys("Drawsko Pomorskie");

		//Throw exception
		try {
			eventFiringWebDriver.findElement(By.id("aaa"));
		}
  		catch (Exception ex) {
  			System.out.println("Rzucono wyjątek " + ex.getCause());
  		}
		
  		//Unregister 
  		eventFiringWebDriver.unregister(myEventListener);
    }
    
    @Test (dependsOnMethods = "WebDriverEvents")
    public void webDriverIO() throws UnsupportedEncodingException{
    	
    	//creating directories
    	try {
			FileHandler.createDir(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises"));
			FileHandler.createDir(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1"));
			FileHandler.createDir(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir2"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//creating files
    	PrintWriter file1txt;
    	PrintWriter file2txt;
    	PrintWriter file11txt;
    	PrintWriter file1rtp;
    	PrintWriter file2rtp;
    	PrintWriter fileToBeRead;
    	
		try {
			System.out.println("Create files, directories.");
			
			//File1.txt
			file1txt = new PrintWriter("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.txt", "UTF-8");
			file1txt.println("The first line");
			file1txt.println("The second line of File1.txt");
			file1txt.close();
	    	
	    	//File2.txt
			file2txt = new PrintWriter("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File2.txt", "UTF-8");
			file2txt.println("The first line");
			file2txt.println("The second line of File2.txt");
			file2txt.close();
			
			//File11.txt
			file11txt = new PrintWriter("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File11.txt", "UTF-8");
			file11txt.println("The first line");
			file11txt.println("The second line of File11.txt");
			file11txt.close();
			
	    	//File1.rtp
			file1rtp = new PrintWriter("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp", "UTF-8");
			file1rtp.println("The first line");
			file1rtp.println("The second line of File1.rtp");
			file1rtp.close();
			
			//File2.rtp
			file2rtp = new PrintWriter("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File2.rtp", "UTF-8");
			file2rtp.println("The first line");
			file2rtp.println("The second line of File2.rtp");
			file2rtp.close();
	    	
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	//copy files, directories
    	try {
    		System.out.println("Copy files, directories.");
    		
    		FileHandler.copy(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp"),
					new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir2\\File1copied.rtp"));
    		
    		Thread.sleep(5000);
    		
    		FileHandler.copy(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1"),
					new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir2"), "1.txt");
    		
    		Thread.sleep(5000);
    		
			FileHandler.copy(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\"),
					new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir2\\"));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//isZipped?
    	System.out.println(
    			"Check if Dir1 is zipped: " +
    			FileHandler.isZipped("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\"));
    	
    	//Make executable
    	try {
    		System.out.println("Make executable.");
    		
    		Thread.sleep(5000);
    		
    		System.out.println("Is executable Dir1: " + FileHandler.canExecute(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp")));
			
			FileHandler.makeExecutable(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp"));
			
			System.out.println("Is executable Dir1 : " + FileHandler.canExecute(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp")));
			
			Thread.sleep(5000);
			System.out.println("Is executable Dir1\\File1.rtp: " + FileHandler.canExecute(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp")));
			
			FileHandler.makeExecutable(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp"));
		
			System.out.println("Is executable Dir1\\File1.rtp: " + FileHandler.canExecute(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp")));
			
    	} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	
    	//Make executable
    	try {
        	System.out.println("Make writable.");
        	
        	Thread.sleep(5000);
    		
			FileHandler.makeWritable(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.rtp"));
			
			Thread.sleep(5000);
			
			FileHandler.makeWritable(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//Read file
		try {
			System.out.println("Read file");
			fileToBeRead = new PrintWriter("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.txt", "UTF-8");
			fileToBeRead.println("FileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeRead"
					+ "FileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeRead"
					+ "FileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeRead"
					+ "FileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeReadFileToBeRead");
			fileToBeRead.println("The second line of FileToBeRead.txt");
			fileToBeRead.close();
			
			String fileContent = FileHandler.readAsString(new File ("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1\\File1.txt"));
			
			System.out.println(fileContent);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//Temporary file system
    	

    	try {
        	//Create directory in TFS
    		System.out.println("Create directory in DFS");
        	File f = TemporaryFilesystem.getDefaultTmpFS().createTempDir("moj", "katalog");
        	System.out.println("Scieżka do DFS: " + f.getAbsolutePath());
			
        	Thread.sleep(5000);
	    	
	    	//Delete directory in TFS
        	System.out.println("Delete directory in DFS");
	    	TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(f);
	    	
	    	//Delete multiple files
	    	System.out.println("Delete multiple files.");
	    	File f1 = TemporaryFilesystem.getDefaultTmpFS().createTempDir("moj1", "katalog");
        	System.out.println("Scieżka do DFS: " + f1.getAbsolutePath());
        	File f2 = TemporaryFilesystem.getDefaultTmpFS().createTempDir("moj2", "katalog");
        	System.out.println("Scieżka do DFS: " + f2.getAbsolutePath());
        	File f3 = TemporaryFilesystem.getDefaultTmpFS().createTempDir("moj3", "katalog");
        	System.out.println("Scieżka do DFS: " + f3.getAbsolutePath());
	    	
        	Thread.sleep(10000);
        	
        	TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        	
        	//Change TFS
        	try {
				FileHandler.createDir(new File("C:\\MyTFS"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        	TemporaryFilesystem myTmpFS = TemporaryFilesystem.getTmpFsBasedOn(new File("C:\\MyTFS"));
        	
        	File file = myTmpFS.createTempDir("moj", "katalog");
        	System.out.println(file.getAbsolutePath());
        	
        	Thread.sleep(5000);
        	
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	//Compressing and decompressing zip files.
    	
    	
    	try {
    		Zip zip = new Zip();
			zip.zip(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir1"),
					new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\zippedDir1.zip"));
			
			Thread.sleep(5000);
			
			zip.unzip(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\zippedDir1.zip"),
					new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\unzippedDir1"));
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//delete file, directory
    	try {
    		FileHandler.delete(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises\\Dir2\\File1copied.rtp"));
        	
			Thread.sleep(5000);
		
			FileHandler.delete(new File("C:\\Documents and Settings\\Wojtek\\Pulpit/Selenium\\IOExcercises"));
    	} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    @Test (dependsOnMethods = "webDriverIO")
    public void seleniumStandaloneServer(){
    	DesiredCapabilities capabilities = new DesiredCapabilities();
    	capabilities.setBrowserName("firefox");
    	RemoteWebDriver remoteWD = null;
    	
    	try {
			remoteWD = new RemoteWebDriver(new URL("http://192.168.1.100:4444/wd/hub"),capabilities);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	remoteWD.get("http://www.google.com");
    	
    	remoteWD.findElement(By.name("q")).sendKeys("Drawsko Pomorskie");
    	
    	remoteWD.findElement(By.name("btnK")).submit();
    }
       
}